/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Quydinh;
import util.HibernateUtil;

/**
 *
 * @author sangnguyen
 */
public class QuyDinhDAO {
    public static boolean InsertQuydinh(Quydinh obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{           
            tx = session.beginTransaction();            
            session.save(obj);
            tx.commit();            
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();
        }                            
        return true;
    }
    public static boolean UpdateQuydinh(Quydinh obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();    
        try{
            session.update(obj);
            tx.commit();
            
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();                            
        }
        return true;
    }
     public static boolean DeleteQuydinh(Quydinh obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            obj.setTinhtrang(0);
            session.update(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();        
        }
        return true;
    }
     public static List<Quydinh> SelectAllQuydinh(){
         List<Quydinh> lsQuydinh = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
         try{
            Query query = session.createQuery("from Quydinh qd where qd.tinhtrang=:tinhTrang order by qd.id asc");
            query.setInteger("tinhTrang", 1);
            lsQuydinh = query.list();         
         }
         catch(HibernateException ex){
             System.err.println(ex);
         }
         finally{
            session.close();       
         }
              
         return lsQuydinh;
     }
     public static Quydinh GetQuyDinhID(int id){
         Quydinh obj = new Quydinh();
         Session session = HibernateUtil.getSessionFactory().openSession();      
         try{
            obj = (Quydinh)session.get(Quydinh.class, id);      
         }
         catch(HibernateException ex){
             System.err.println(ex);
         }
         finally{
             session.close();        
         }         
         return obj;
     }
}
